using System;
using System.Collections.Generic;
using System.Linq;

using Csla;

using EntModApp.EntServices.Business;
using EntModApp.EntServices.IoC;

using DemoApp.DataAccess;

namespace DemoApp.BusinessLayer 
{
  [Serializable]
  public partial class CslaPersonEdit : 
    CslaUserEdit,
    IPerson
  {
    #region [ Business Properties ]

    public static PropertyInfo<string> FirstNamePropertyInfo = RegisterProperty(new PropertyInfo<string>("FirstName"));
    public string FirstName 
    { 
      get
      {
        return GetProperty(FirstNamePropertyInfo);
      }
      set
      {
        LoadProperty(FirstNamePropertyInfo, value);
      }
    }

    public static PropertyInfo<string> LastNamePropertyInfo = RegisterProperty(new PropertyInfo<string>("LastName"));
    public string LastName 
    { 
      get
      {
        return GetProperty(LastNamePropertyInfo);
      }
      set
      {
        LoadProperty(LastNamePropertyInfo, value);
      }
    }

    public static PropertyInfo<string> EmailPropertyInfo = RegisterProperty(new PropertyInfo<string>("Email"));
    public string Email 
    { 
      get
      {
        return GetProperty(EmailPropertyInfo);
      }
      set
      {
        LoadProperty(EmailPropertyInfo, value);
      }
    }

    #endregion [ Business Properties ]

    #region [ Validation Rules ]
    #endregion [ Validation Rules ]

    #region [ Authorization Rules ]
    #endregion [ Authorization Rules ]
	
    #region [ Factory Methods ]

    #region [ Root Factory Methods ]
    public static CslaPersonEdit New()
    {
      return DataPortal.Create<CslaPersonEdit>();
    }

    public static void Delete(UserKey key)
    {
      DataPortal.Delete<Person>(new SingleCriteria<Person, UserKey>(key));
    }

    public static void Delete(Guid UserId)
    {
      Delete(new UserKey { UserId = UserId });
    }
    #endregion [ Root Factory Methods ]

    #region [ Child Factory Methods ]
    internal static CslaPersonEdit NewChild()
    {
      return DataPortal.CreateChild<CslaPersonEdit>();
    }

    internal static CslaPersonEdit GetPersonEdit(Person data, PersonLoadSettings loadSettings)
    {
      if (data == null) throw new ArgumentNullException("data");
      if (loadSettings == null) throw new ArgumentNullException("loadSettings");
      CslaPersonEdit result = new CslaPersonEdit();
      result.FetchPerson(data, loadSettings);
      result.MarkAsChild();
      return result;
    }
    #endregion [ Child Factory Methods ]

    private static int ForceInitField = 0;
    protected CslaPersonEdit() 
    {
      ForceInitField = 1;
    }
    #endregion [ Factory Methods ]

    #region [ Data Access ]

    #region [ Root Data Access ]
    protected override void DataPortal_Create()
    {
      base.DataPortal_Create();
    }

    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Insert()
    {
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_Update()
    {
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    protected override void DataPortal_DeleteSelf()
    {
      DataPortal_Delete(new SingleCriteria<Person, UserKey>(new UserKey { UserId = this.UserId }));
    }
    
    [Transactional(TransactionalTypes.TransactionScope)]
    private void DataPortal_Delete(SingleCriteria<Person, UserKey> criteria)
    {
    }
    #endregion [ Root Data Access ]

    #region [ Child Data Access ]
    protected override void Child_Create()
    {
      base.Child_Create();
    }
    
    private void Child_Fetch(object childData)
    {
    }
    
    private void Child_Insert(object parent)
    {
    }
    
    private void Child_Update(object parent)
    {
    }
    
    private void Child_DeleteSelf(object parent)
    {
    }
    #endregion [ Child Data Access ]

    protected void FetchPerson(Person data, PersonLoadSettings loadSettings)
    {
      FetchPersonData(data, loadSettings);
      FetchPersonChildren(data, loadSettings);
    }

    protected void FetchPersonData(Person data, PersonLoadSettings loadSettings)
    {
      base.FetchUserData(data, loadSettings);
      this.FirstName = data.FirstName;
      this.LastName = data.LastName;
      this.Email = data.Email;
    }

    protected void FetchPersonChildren(Person data, PersonLoadSettings loadSettings)
    {
      base.FetchUserData(data, loadSettings);
    }
    #endregion [ Data Access ]

    #region [ Explicit IPerson implementation ]

    string IPerson.FirstName
    {
      get { return this.FirstName; }
      set
      {
        this.FirstName = value;
      }
    }

    string IPerson.LastName
    {
      get { return this.LastName; }
      set
      {
        this.LastName = value;
      }
    }

    string IPerson.Email
    {
      get { return this.Email; }
      set
      {
        this.Email = value;
      }
    }


    bool IBusinessObject.IsReadonly
    {
      get { return false; }
    }

    bool IBusinessObject.IsDirty
    {
      get { return this.IsDirty; }
    }

    #endregion [ Explicit IPerson implementation ]
  }
}
